<?php

/**
 * Abstract
 * 作者: qinzhi
 */
class Controller_Seller_Abstract extends FController
{

    public $openid;

    public $clerk;

    public function beforeAction()
    {
        global $_F;

        /*$_F['uid'] = 2;
        $_F['shop_id'] = 3;*/

        $_F['s_url'] = 'http://img1.yuanfenba.net';
        $_F['s_url_oss'] = FConfig::get('global.s_url_oss');

        if(FCookie::get('sopenid')){
            $this->openid = FCookie::get('sopenid');
            $this->clerk = Service_Clerk::getClerkInfo($this->openid);
        }else{
            require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
            $options = array(
                'token' => FConfig::get('wechat.seller.token'),
                'appid' => FConfig::get('wechat.seller.appid'),
                'appsecret' => FConfig::get('wechat.seller.appsecret'),
            );
            $weixin = new WeixinChat($options);
            if(FRequest::getString('code') != ''){
                $code = FRequest::getString('code');
                $result = $weixin->getSnsAccessToken($code);
                $this->openid = $result['openid'];
                /*if(empty($this->openid)){
                    header("Content-type: text/html; charset=utf-8");
                    die('获取用户基本信息失败!');
                }
                $user_token = $result['access_token'];//用户令牌

                $result = $weixin->getSnsUserInfo($user_token,$this->openid);*/
                $clerk = Service_Clerk::getClerkInfo($this->openid);
                if(empty($clerk)) die('404');

                $this->clerk = $clerk;
                FCookie::set('sopenid',$this->openid,30*86400);
            }else{
                $weixin->redirectGetOauthCode($this->getFullUrl());
            }
        }

        if(empty($this->clerk)) $this->error('用户不存在');
        elseif($this->clerk['status'] == 2) $this->error('你的账户已被禁用');
        elseif($this->clerk['status'] == -1) $this->error('你的账户已被删除');

        $_F['uid'] = $this->clerk['uid'];
        $_F['shop_id'] = $this->clerk['shop_id'];



        $this->assign('clerk',$this->clerk);

        return true;
    }

    /**
     * @return string
     */
    function getFullUrl(){
        global $_F;
        $url = 'http://' . $_F['http_host'] . $_F['uri'];
        if($_F['query_string'] != ''){
            $url .= '?' . $_F['query_string'];
        }
        return $url;
    }

    function showMessage($message, $messageType = 'success', $jumpUrl = null)
    {
        if ($messageType == 'error') {
            $messageType = 'warning';
        }
        $this->assign('messageType', $messageType);
        $this->assign('message_content', $message);
        $this->assign('jump_url', $jumpUrl);
        $this->display('admin/message');
        exit(0);
    }

    function display($tpl = null)
    {
        parent::display(str_replace('seller/', '', $tpl));
    }

}
